# -*- coding: utf-8 -*-
import re

import requests

import cookie
from 退役军人一件事获取虚拟事项 import 获取虚拟事项

class SetDispatch:
    ur='59.215.206.188'
    dispatch='ddglzy'
    cookie = cookie.cookie
    header={
        "Host": "{}".format(ur),
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:92.0) Gecko/20100101 Firefox/92.0",
        "Accept": "application/json, text/javascript, */*; q=0.01",
        "Accept-Language": "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2",
        "Accept-Encoding": "gzip, deflate",
        "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
        "X-Requested-With": "XMLHttpRequest",
        "Content-Length": "311",
        "Origin": "http://{}".format(ur),
        "Connection": "keep-alive",
        #"Referer": "http://59.215.206.188/ddglzy/dispatch/resourse/itemconfigure/index",
        "Cookie": "{}".format(cookie).encode("utf-8"),
    }

    #查询调度系统
    def query(self,code,regCode):

        url='http://{}/{}/dispatch/resourse/itemconfigure/query'.format(self.ur,self.dispatch)
        data={
            "itemcode": "{}".format(code),
            "itemname": "",
            "regName": "",
            "regCode": "{}".format(regCode),
            # "itemname": "房地产开发项目前期物业服务合同备案",
            # "regName": "遵义市",
            # "regCode": "520300",
            "orgName": "",
            "orgCode": "",
            "type": "",
            "_search": "true",
            "nd": "",
            "rows": "10",
            "page": "1",
            "sidx": "sort",
            "sord": "asc",
        }
        x=requests.post(url,data=data,headers=self.header).json()
        #print(x)
        itemId=x['rows'][0]['cell']['ID']
        exchangeCode=itemCode=x['rows'][0]['cell']['CODE']
        organName=x['rows'][0]['cell']['ORG_NAME']
        organCode=x['rows'][0]['cell']['ORG_CODE']
        version=x['rows'][0]['cell']['VERSION']
        areaCode=x['rows'][0]['cell']['REGION_CODE']
        areaName=x['rows'][0]['cell']['REGION_NAME']
        itemTime=x['rows'][0]['cell']['AGREE_TIME']
        NAME=x['rows'][0]['cell']['NAME']
        #print(itemId,exchangeCode,itemCode,organName,organCode,version,areaCode,areaName,itemTime,NAME)
        return itemId,exchangeCode,itemCode,organName,organCode,version,areaCode,areaName,itemTime,NAME

    #获取事项ID
    def getID(self,code,regCode):
        head={
            "Host": "{}".format(self.ur),
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:92.0) Gecko/20100101 Firefox/92.0",
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
            "Accept-Language": "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2",
            "Accept-Encoding": "gzip, deflate",
            "Connection": "keep-alive",
            "Referer": "http://{}/{}/dispatch/resourse/itemconfigure/index".format(self.ur,self.dispatch),
            "Cookie": "{}".format(self.cookie).encode("utf-8"),
            "Upgrade-Insecure-Requests": "1",
        }
        queryRe=SetDispatch().query(code,regCode)

        url='http://{}/{}/dispatch/resourse/itemconfigure/itemshow?itemid={}&itemcode={}&version={}'.format(self.ur,self.dispatch,queryRe[0],queryRe[2],queryRe[5])

        x=requests.get(url,headers=head).text
        match =re.findall(r'value="(.*?)"\s+name="id"', x)
        #print(match)
        return match[0]
    #设置调度信息
    def updata(self,code,regCode):
        queryIdRe=SetDispatch().getID(code,regCode)
        queryRe=SetDispatch().query(code,regCode)

        url='http://{}/{}/dispatch/resourse/itemconfigure?method=save'.format(self.ur,self.dispatch)
        form审批={"id":"{}".format(queryIdRe),"itemId":"{}".format(queryRe[0]),"itemCode":"{}".format(queryRe[2]),
              "areaCode":"{}".format(queryRe[6]),"areaName":"{}".format(queryRe[7]),"itemTime":"{}".format(queryRe[8]),
              "organName":"{}".format(queryRe[4]),"organCode":"{}".format(queryRe[5]),"exchangeCode":"{}".format(queryRe[1]),
              "sysInfo":[{"sysId":"A7875618DB3B46F1993D91BA606B582A","sysType":"dispatch","sysName":"产品调度系统3.5"},{"sysType":"queuing","sysName":""},{"sysType":"punlish","sysName":""},{"sysId":"74AAC186781B42099DD1E0639A772E2F","sysType":"online","sysName":"网厅网上申报系统"},{"sysType":"hallManager","sysName":""},{"sysId":"B1C13E3EC16D477C991E6ECB4D2F5747","sysType":"accept","sysName":"业务中台"},{"sysId":"AAF8F71291C144AB9DBB7A79025E69DF","sysType":"approve","sysName":"公共审批系统3.5"}]}

        form退役军人一件事社保卡申领= {"id": "{}".format(queryIdRe), "itemId": "{}".format(queryRe[0]),
                    "itemCode": "{}".format(queryRe[2]),
                    "areaCode": "{}".format(queryRe[6]), "areaName": "{}".format(queryRe[7]),
                    "itemTime": "{}".format(queryRe[8]),
                    "organName": "{}".format(queryRe[4]), "organCode": "{}".format(queryRe[5]),
                    "exchangeCode": "{}".format(queryRe[1]),
                    "sysInfo": [{"sysId": "A7875618DB3B46F1993D91BA606B582A", "sysType": "dispatch",
                                 "sysName": "产品调度系统3.5"}, {"sysType": "queuing", "sysName": ""},
                                {"sysType": "punlish", "sysName": ""},
                                {"sysId": "74AAC186781B42099DD1E0639A772E2F", "sysType": "online",
                                 "sysName": "网厅网上申报系统"}, {"sysType": "hallManager", "sysName": ""},
                                {"sysId": "B1C13E3EC16D477C991E6ECB4D2F5747", "sysType": "accept",
                                 "sysName": "业务中台"},
                                {"sysId": "D47AE26635F7483B975205C0E4F418B6", "sysType": "approve",
                                 "sysName": "退役军人一件事社保卡申领"}]}
        if (queryRe[9]=='社会保障卡（含电子社保卡）申领'):
            date=form退役军人一件事社保卡申领
        else:
            date=form审批
        x=requests.post(url,json=date,headers=self.header).json()
        print(queryRe[7]+">"+queryRe[-1]+":"+str(x['statusCode']))
    #按区划循环
    def loop(self):
        org = {

            "遵义市": "520300",

            "红花岗区": "520302",
            "汇川区": "520303",
            "播州区": "520321",
            "桐梓县": "520322",
            "绥阳县": "520323",
            "正安县": "520324",
            "道真县": "520325",
            "务川县": "520326",
            "凤冈县": "520327",
            "湄潭县": "520328",
            "余庆县": "520329",
            "习水县": "520330",
            "赤水市": "520381",
            "仁怀市": "520382",
            "新蒲新区": "520383",

        }


        for org,code in org.items():



            for i in 获取虚拟事项().start(regionCode=code):


                SetDispatch().updata(i,code)


#SetDispatch().query('00A36A4247524DCFBCE6FAD3A177E42E','520302')
#SetDispatch().updata('00A36A4247524DCFBCE6FAD3A177E42E','520302')
#SetDispatch().getID('00A36A4247524DCFBCE6FAD3A177E42E','520302')
SetDispatch().loop()
